<?php

namespace paymch\controllers;

use Yii;
use paymch\models\AlipayPayment;
use paymch\models\WechatPayment;
use paymch\models\WechatRefund;
use paymch\components\wechat\HttpDataFormatter;

/**
 * 用于支付平台异步回调通知
 * NotifyController
 * 
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class NotifyController extends \paymch\components\BaseController {

    /**
     * 支付宝异步通知
     * @return string
     */
    public function actionAlipay() {
        $post = Yii::$app->request->post();

        //记录日志-保存支付宝异步通知参数
        parent::log('AlipayPayment', [
            'postData' => $post
        ]);
        $model = new AlipayPayment();
        $msg = 'error';
        if ($model->load($post, '')) {
            $tradeid = $model->save($msg);
            if ($tradeid !== false) {
                $msg = 'success';
                $this->setTradeId($tradeid);
            }
        }
        return $msg;
    }

    /**
     * 微信支付异步通知
     * @return string
     */
    public function actionWechat() {
        $xml = Yii::$app->request->getRawBody();
        $data = HttpDataFormatter::fromXml($xml);
        if ($data === false) {
            //记录日志-保存通知参数
            parent::log('WechatPayment', [
                'xml' => $xml
            ]);
            return $this->responseToWechat('请求数据无法被获取', false);
        }

        //记录日志-保存支付宝异步通知参数
        parent::log('WechatPayment', [
            'postData' => $data
        ]);

        $model = new WechatPayment();
        $model->setCouponFees($data);
        if ($model->load($data, '')) {
            $msg = '';
            $tradeid = $model->save($msg);
            if ($tradeid === false) {
                return $this->responseToWechat($msg ?: implode(' ', $model->firstErrors), false);
            }
            $this->setTradeId($tradeid);
            return $this->responseToWechat();
        }

        return $this->responseToWechat('请求数据无法被获取', false);
    }

    /**
     * 微信支付退款异步通知
     *
     * @return string
     */
    public function actionWechatRefund() {
        $xml = Yii::$app->request->getRawBody();
        $data = HttpDataFormatter::fromXml($xml);
        if ($data === false) {
            //记录日志-保存通知参数
            parent::log('WechatRefund', [
                'xml' => $xml
            ]);
            return $this->responseToWechat('请求数据无法被获取', false);
        }

        //记录日志-保存支付宝异步通知参数
        parent::log('WechatRefund', [
            'postData' => $data
        ]);

        $model = new WechatRefund();
        if ($model->load($data, '')) {
            $msg = '';
            $tradeid = $model->save($msg);
            if ($tradeid === false) {
                return $this->responseToWechat($msg ?: implode(' ', $model->firstErrors), false);
            }
            $this->setTradeId($tradeid);
            return $this->responseToWechat();
        }

        return $this->responseToWechat('请求数据无法被获取', false);
    }

    /**
     * 格式化处理微信响应内容
     *
     * @return mixed
     */
    protected function responseToWechat($message = 'OK', $status = true) {
        Yii::error("\nResponseToWechat message is：" . $message, 'payment');
        return HttpDataFormatter::toXml([
            'return_code' => $status ? 'SUCCESS' : 'FAIL',
            'return_msg' => $message
        ]);
    }

}
